#include "stdafx.h"
#include "clipboard.h"

void CopyTextToClipboard(LPCSTR text)
{
	if( OpenClipboard(NULL) == FALSE)
		return;

	EmptyClipboard();

	HGLOBAL hClipboardData;
	hClipboardData = GlobalAlloc( GMEM_DDESHARE, strlen( text) + 1);
	char* pchData;
	pchData = (char*)GlobalLock(hClipboardData);
	if (SF_strcpy( pchData, strlen( text) + 1, text) != S_OK)
	{
		_ASSERT(FALSE);
	}

	GlobalUnlock( hClipboardData);

	SetClipboardData(CF_TEXT, hClipboardData);

	CloseClipboard();
}

inline bool IsRangedChar(char c, char c_begin, char c_end)
{
	return (c <= c_end && c >= c_begin);
}

void CopyTextToClipboardEx(LPCSTR text, bool bRemoveColorCode)
{
	if(!bRemoveColorCode) 
	{
		CopyTextToClipboard(text);
	}
	else
	{
		int nLen = strlen(text);
		LPSTR szBuffer = new char[nLen+1];
		memset(szBuffer, 0, nLen+1);
		
		LPSTR pTemp = (LPSTR)text;
		int nIndex = 0;

		for(int i = 0; i < nLen; )
		{
			if(*pTemp == '^' && IsRangedChar(*(pTemp+1), '0', '9'))
			{
				pTemp++;
				i++;
			}
			else
			{
				szBuffer[nIndex++] = *pTemp;				
			}

			pTemp++;
			i++;
		}

		CopyTextToClipboard(szBuffer);

		delete [] szBuffer;
	}
}
